<?php
// +----------------------------------------------------------------------
// | 生态墓销售流程
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class FileController extends AdminBaseController
{

    public function initialize()
    {
        $this->user_model = Db::name("user");
        $this->dept_model = Db::name("department");
        $this->file_model = Db::name('file');
        parent::initialize();
    }

    /**
     * 档案管理
     * @return mixed
     */
    public function index()
    {
        $file_num = $this->request->param("file_num");
        $tomb_num = $this->request->param("tomb_num");
        $customer_name = $this->request->param("customer_name");
        $start_time = $this->request->param("start_time");
        $end_time = $this->request->param("end_time");
        if (empty($end_time)) {
            $end_time = 99999999999;
        }

        $n1 = 'DESC';
        $n2 = 'ASC';
        //dump($n1);
        //$name = $_GET['btnname'];
        //dump($name);exit;

        $results = $this->file_model->alias('f')->field('f.*,fb.customer_name,fb.customer_phone')
            ->leftjoin('dj_file_buyer fb', 'fb.file_id=f.id')
            ->where('file_num', 'like', '%' . $file_num . '%')
            ->where('tomb_num', 'like', '%' . $tomb_num . '%')
            ->where('customer_name', 'like', '%' . $customer_name . '%')
            ->whereTime('f.closing_time', '>=', $start_time)
            ->whereTime('closing_time', '<=', $end_time)
            ->order('tomb_num', $n2)
            //->order('file_num',$sort)
            //->order('closing_time',$sort)
            ->paginate(15);


        $page = $results->render();
        $this->assign("results", $results);
        $this->assign("page", $page);
        return $this->fetch();
    }

    /*档案添加--跳转*/
    public function add()
    {
        $results = Db::name("user")->alias('u')->field('u.*,d.department_name')
            ->join('dj_department d', 'd.id=u.dept_id')
            ->order('d.department_name', 'DESC')
            ->select();

        $this->assign("users", $results);
        $this->assign('user_nickname', cmf_get_admin_name());
        return $this->fetch();
    }

    /*档案添加--保存*/
    public function add_post()
    {
        $data = $this->request->param();

        //前台获取要插入的数据
        $new_data = array(
            'handle' => cmf_get_current_admin_id(),
            'file_num' => $data['file_num'],
            'tomb_num' => $data['tomb_num'],
            'tomb_region' => $data['tomb_region'],
            'tomb_address' => $data['tomb_address'],
            'sell_price' => $data['sell_price'],
            'discount' => $data['discount'],
            'tomb_type' => $data['tomb_type'],
            'closing_price' => $data['closing_price'],

            'shoppingguide' => $data['shoppingguide'],
            'agent_user' => $data['agent_user'],
            'reaching' => $data['reaching'],
            'handle' => cmf_get_current_admin_id()
        );

        $closing_time = $data['closing_time'];

        $new_data['closing_time'] = strtotime($closing_time);


        $id = Db::name('file')->insertGetId($new_data);

        if ($id != false) {
            $this->file_buyer_add($id);
            $this->file_deceased_add($id);
            $this->success("添加成功", url('File/index'));
        } else {
            $this->error("添加失败");
        }
    }

    /*购买人信息添加*/
    public function file_buyer_add($file_id)
    {
        $album = array(
            'file_id' => $file_id,
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
            'customer_name' => $_POST['customer_name'],
            'customer_phone' => $_POST['customer_phone'],
            'customer_account' => $_POST['customer_account'],
            'cart_ID' => $_POST['cart_ID'],
            'cart_type' => $_POST['cart_type'],
            'contact_address' => $_POST['contact_address'],
        );
        Db::name('file_buyer')->insert($album);
    }

    /*逝者信息添加*/
    public function file_deceased_add($file_id)
    {
        $deceased_name = $_POST['deceased_name'];
        $gender = $_POST['gender'];

        $date_birth = $_POST['date_birth'];
        $date_death = $_POST['date_death'];
        $date_funeral = $_POST['date_funeral'];
        $funeral_time = $_POST['funeral_time'];

        $convergence_type = $_POST['convergence_type'];
        $type = $_POST['type'];
        $receptionist = $_POST['receptionist'];
        $relationship_users = $_POST['relationship_users'];

        for ($i = 0; $i < count($deceased_name); $i++) {
            $album[$i]['file_id'] = $file_id;
            $album[$i]['add_time'] = time();
            $album[$i]['add_user'] = cmf_get_current_admin_id();
            $album[$i]['deceased_name'] = $deceased_name[$i];
            $album[$i]['gender'] = $gender[$i];

            //时间类型，先获取值再转
            $album[$i]['date_birth'] = strtotime($date_birth[$i]);
            $album[$i]['date_death'] = strtotime($date_death[$i]);
            $album[$i]['date_funeral'] = strtotime($date_funeral[$i]);
            $album[$i]['funeral_time'] = strtotime($funeral_time[$i]);

            $album[$i]['convergence_type'] = $convergence_type[$i];
            $album[$i]['gender'] = $gender[$i];
            $album[$i]['type'] = $type[$i];
            $album[$i]['receptionist'] = $receptionist[$i];
            $album[$i]['relationship_users'] = $relationship_users[$i];
        }
        Db::name("file_deceased")->insertAll($album);
    }

    /*档案编辑跳转*/
    public function edit()
    {
        $id = $this->request->param("id");
        $where = array(
            'id' => $id
        );

        $this->file_buyer($id);
        $this->file_deceased($id);
        $this->dept_per();
        $result = Db::name("file")
            ->where($where)
            ->find();

        $this->assign($result);
        $this->assign('result', $result);
        $this->assign('user_name', cmf_get_admin_name());

        return $this->fetch();
    }

    /*编辑-获取购买人表*/
    public function file_buyer($file_id)
    {

        $result = Db::name("file_buyer")
            ->where('file_id', $file_id)
            ->find();
        $this->assign($result);
    }

    /*编辑-获取逝者信息表*/
    public function file_deceased($file_id)
    {

        $convergence_type = $this->request->param('convergence_type');

        $result = Db::name("file_deceased")->alias('fd')
            ->field('fd.id,fd.deceased_name,fd.gender,fd.date_birth,fd.date_death,
                fd.date_funeral,fd.funeral_time,fd.convergence_type,fd.type,fd.receptionist,fd.relationship_users')
            ->where('fd.file_id', $file_id)
            ->select();

        $this->assign('deceased', $result);
    }

    /*编辑-获取部门和人员*/
    public function dept_per()
    {
        $results = Db::name("user")->alias('u')->field('u.*,d.department_name')
            ->join('dj_department d', 'd.id=u.dept_id')
            ->order('d.department_name', 'DESC')
            ->select();

        $this->assign('deptPer', $results);
    }

    /*档案编辑保存*/
    public function edit_post()
    {
        $id = $this->request->param('id');
        $data = $this->request->param();

        $data = array(
            'tomb_num' => $data['tomb_num'],
            'tomb_address' => $data['tomb_address'],
            'sell_price' => $data['sell_price'],
            'shoppingguide' => $data['shoppingguide'],
            'file_num' => $data['file_num'],
            'tomb_region' => $data['tomb_region'],
            'discount' => $data['discount'],
            'tomb_type' => $data['tomb_type'],
            'closing_time' => strtotime($data['closing_time']),
            'closing_price' => $data['closing_price'],
            'agent_user' => $data['agent_user'],
            'reaching' => $data['reaching'],
            'add_time' => time(),
        );

        Db::name('file')
            ->where('id', $id)
            ->update($data);

        $this->buyerEditPost($id);
        $this->deceasedEditPost($id);

        //echo Db::name('file_deceased')->getLastSql();

        $this->success("保存成功", url('file/index'));
    }

    /*购买人信息--编辑保存*/
    public function buyerEditPost($file_id)
    {
        $data = array(
            'customer_name' => $_POST['customer_name'],
            'customer_phone' => $_POST['customer_phone'],
            'customer_account' => $_POST['customer_account'],
            'cart_type' => $_POST['cart_type'],
            'cart_ID' => $_POST['cart_ID'],
            'contact_address' => $_POST['contact_address'],
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
        );

        Db::name('file_buyer')
            ->where('file_id', $file_id)
            ->update($data);
    }

    /*逝者信息--编辑保存*/
    public function deceasedEditPost($file_id)
    {
        $album = array(
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
        );
        //取name值
        $id = $this->request->param("de_id");

        $deceased_name = $_POST['deceased_name'];
        $gender = $_POST['gender'];
        $convergence_type = $_POST['convergence_type'];
        $type = $_POST['type'];
        $receptionist = $_POST['receptionist'];
        $relationship_users = $_POST['relationship_users'];

        //$_POST请求数据库字段
        $date_birth = $_POST['date_birth'];
        $date_death = $_POST['date_death'];
        $date_funeral = $_POST['date_funeral'];
        $funeral_time = $_POST['funeral_time'];

        for ($i = 0; $i < count($id); $i++) {
            $album['id'] = $id[$i];
            $album['deceased_name'] = $deceased_name[$i];
            $album['gender'] = $gender[$i];
            $album['convergence_type'] = $convergence_type[$i];
            $album['gender'] = $gender[$i];
            $album['type'] = $type[$i];
            $album['receptionist'] = $receptionist[$i];
            $album['relationship_users'] = $relationship_users[$i];
            //时间类型，先获取值再转
            $album['date_birth'] = strtotime($date_birth[$i]);
            $album['date_death'] = strtotime($date_death[$i]);
            $album['date_funeral'] = strtotime($date_funeral[$i]);
            $album['funeral_time'] = strtotime($funeral_time[$i]);
            Db::name("file_deceased")->where('id', $id[$i])->update($album);
        }
        //echo Db::name("file_deceased")->getLastSql();
    }

    /*档案删除*/
    public function delete()
    {
        $id = $this->request->param("id");

        if ($id != '' || $id != null) {
            Db::name("file_deceased")->where('file_id', $id)->delete();
            Db::name("file_buyer")->where('file_id', $id)->delete();
            Db::name("file")->where('id', $id)->delete();
            $this->success("删除成功", url('file/index'));
        } else {
            $this->error("数据出错", url('file/index'));
        }

    }

}